/**
 * 项目：ystree
 * 模块：
 * 作者：yangjianliang
 * 日期：2007-6-22
 */
package net.ysen.tree.abst;

import java.util.List;

import net.ysen.tree.ITree;
import net.ysen.tree.Consts.ExpressCodeType;
import net.ysen.tree.entitybean.TreeBean;

public abstract class TreeAbstract implements ITree {
	
	public static final String lineSeparator = System.getProperty("line.separator");
	
	/**
	 * 把查询出的数据转化为TreeBean对象。
	 * @param list
	 * @return
	 */
	public abstract List<TreeBean> treeBeanConversion(List list);
	
	public abstract String getTreeHtml(TreeBean tree);

	public abstract String getTreeXML(TreeBean tree);
	
	public final TreeBean loadTreeData(List list){
		List<TreeBean> treeList = treeBeanConversion( list );
		return boxTreeData( treeList );
	}
	
	/*
	 * (non-Javadoc)
	 * @see net.ysen.tree.ITree#getTreeExpressCode(net.ysen.tree.entitybean.TreeBean)
	 */
	public final String getTreeExpressCode(TreeBean tree, ExpressCodeType expressCodeType){
		if( expressCodeType == ExpressCodeType.HTML ){
			return getTreeHtml(tree);
		}else if( ExpressCodeType.XML == expressCodeType ){
			return getTreeXML(tree);
		}else{
			return getTreeHtml(tree);
		}
		
	}
	
	/**
	 * 把list装载成树。这样实际就形成了一颗对象树结构
	 * @param sons
	 * @return
	 */
	protected final TreeBean boxTreeData(List<TreeBean> sons){
		
		TreeBean tree = null;
		
		for(int i=0,k=sons.size();i<k;i++){
			TreeBean treeNode = sons.get(i);
			TreeBean parentNode = findNode(sons, treeNode.getParentid());
			if( parentNode!=null ){
				parentNode.addSonNode(treeNode);
				if( TreeBean.isShowCheckBox() && parentNode.isChecked() ){//父节点被选中，全部子节点也要选中
					treeNode.setChecked(true);
				}
			}else{//没有父节点，说明是根节点
				tree = treeNode;
				tree.setExpandNode(true);
				tree.setRootNode(true);
				
			}
		}
		findTreeEndNode( tree ).setEndNode(true);//树的最后一个节点
		
		return tree;
	}
	
	/**
	 * 根据ID查询某个节点
	 * @param treeList
	 * @param treeNodeId
	 * @return
	 */
	protected final TreeBean findNode(List<TreeBean> treeList, final String treeNodeId){
		if(treeList==null || treeList.isEmpty() || treeNodeId==null || treeNodeId.equals("")){
			return null;
		}
		TreeBean findNode = null;
		for(int i=0,k=treeList.size();i<k;i++){
			TreeBean treeNode = treeList.get(i);
			if(treeNodeId.equals(treeNode.getId())){
				findNode = treeNode;
				break;
			}
			findNode = findNode( treeNode.getSonNode(), treeNodeId);
			if(findNode!=null)break;
		}
		return findNode;
	}

	/**
	 * 查找树的最后一个节点
	 * @param treeList
	 * @param id
	 * @return
	 */
	protected final TreeBean findTreeEndNode(TreeBean tree){
		if( tree==null ){
			return null;
		}
		TreeBean treeEnd = findEndNode( tree.getSonNode() );
		return treeEnd==null?tree:treeEnd;
	}

	/**
	 * 查找树的最后一个节点
	 * @param treeList
	 * @param id
	 * @return
	 */
	private final TreeBean findEndNode(List<TreeBean> treeList){
		if(treeList==null || treeList.isEmpty() ){
			return null;
		}
		TreeBean findNode = null;
		TreeBean treeNode = treeList.get( treeList.size()-1 );
		if( treeNode.hasSonNode() ){
			findNode = findEndNode( treeNode.getSonNode() );
		}else{
			findNode = treeNode;
		}
		return findNode;
	}

	/**
	 * 装载数据到树 要求记录集是有顺序的
	 * @param list
	 */
	/*
	protected TreeBean boxTreeData(List list){
		
		List<TreeBean> sons = new ArrayList<TreeBean>();
		for(int i=0,j=list.size();i<j;i++){
			Object[] o = (Object[])list.get(i);
			TreeBean treeson = new TreeBean();
			treeson.setId(o[1].toString());
			treeson.setParentid(o[2].toString());
			treeson.setText(o[0].toString());
			treeson.setValue(o[5].toString());
			sons.add(treeson);
		}
		
		TreeBean tree = sons.get(0);
		tree.setExpandSonNode(true);
		tree.setRootNode(true);
		
		for(int i=1,k=sons.size();i<k;i++){
			TreeBean treeson = sons.get(i);
			TreeBean treesonPrev = sons.get(i-1);
			
			if(treeson.getParentid().equals( treesonPrev.getId() )){//是上一节点的子节点
				treesonPrev.addSonNode(treeson);
				if( TreeBean.isShowCheckBox() && treesonPrev.isChecked() ){//父节点被选中，全部子节点也要选中
					treeson.setChecked(true);
				}
			}
			else {//和上一节点同级 或者 是上一节点的叔父节点
				TreeBean parentNode = findNode(sons, treeson.getParentid());
				parentNode.addSonNode(treeson);
			}
			if( i==k-1){//是树的最后一个节点
				treeson.setEndNode(true);
			}
		}

		return tree;
	}
	*/
}
